<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Facebookapi{
	
	function __construct() {
		$this->ci =& get_instance();
		$this->ci->load->config();
		$this->ci->load->config('stareit', TRUE);
		//$this->ci->load->library('security');
		$this->ci->load->library('session');
		$this->ci->load->model('stareit/connection');
	}
	
	public function login(){
		$app_id = $this->ci->config->item('fb_app_id', 'stareit');
        $app_secret = $this->ci->config->item('fb_app_secret', 'stareit');
		$my_url = $this->ci->config->item('redirect_uri', 'stareit');
        
		$login_url = $this->ci->config->item('login_url', 'stareit');
        $token_url = $this->ci->config->item('token_url', 'stareit');
		$graph_url = $this->ci->config->item('graph_url', 'stareit');
        $get_data = $this->ci->input->get(NULL, TRUE);
		$code = @$get_data['code'];
		$user_data = $this->ci->session->userdata('user_data');//get session
		
        if (empty($code)) {
            /*
             * 登入維持24小時
             */
            $expire_minute = 60 * 60 * 24;
            
            /*
             * 超過登入時效
             */
            if (isset($user_data['LOGIN_INFO']['LAST_LOGIN_TIME']) && (time() - $user_data['LOGIN_INFO']['LAST_LOGIN_TIME'] > $expire_minute)) {
            	$this->ci->session->sess_destroy();// destroy session data in storage
				
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            
            }
            
            /*
             * 尚未login
             */
            
            if (!isset($user_data['LOGIN_INFO']['LAST_LOGIN_TIME']) || !isset($user_data['LOGIN_INFO']['LOGIN_CODE'])) {
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            }
        
            
            if ( empty($code) && !isset($user_data['LOGIN_INFO']['LOGIN_CODE'])) {
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            }
        }
		
        
        
		/*
		 * 確認login code 正確
		 * 並拉回user info
		 */
		
		$code = isset($get_data["code"])?$get_data["code"]:$user_data['LOGIN_INFO']['LOGIN_CODE'];
		
        $token_url = $token_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&expires_in=86400&code=".$code;
        
        $access_token = @file_get_contents($token_url);
        
        $graph_url = $graph_url.$access_token;
        
        $user = json_decode(@file_get_contents($graph_url));
		/*
		var_dump($token_url);
		echo "<br>";
		var_dump($access_token);
		echo "<br>";
		*/
		if(isset($user->id)){
			/*
			 * 確認登入成功
			 */
			//error_log('login success');
			$newdata = array();
			$newdata['LOGIN_INFO']['LOGIN_CODE'] = $code;
        	$newdata['LOGIN_INFO']['LAST_LOGIN_TIME'] = time();
			$newdata['LOGIN_INFO']['id'] = $user->id;
			$newdata['LOGIN_INFO']['name'] = $user->name;
			
			$this->ci->session->set_userdata('user_data',$newdata);
			
			$this->ci->connection->checkIfUserSetUp($user->id);
			
		}
		else{
			$user = false;
		}
		
		
		return $user;
	}
	
	function logout(){
		$user_data = $this->ci->session->userdata('user_data');//get session
		
		$this->ci->session->sess_destroy();// destroy session data in storage
		//echo("<script> top.location.href='".LOGOUT_URL."'</script>");
		//echo ($this->ci->config->item('logout_url', 'stareit')).'&session_key='.$user_data['LOGIN_INFO']['LOGIN_CODE'];
		echo("<script> top.location.href='".($this->ci->config->item('logout_url', 'stareit')).'&session_key='.$user_data['LOGIN_INFO']['LOGIN_CODE']."'</script>");
		exit;
	}
	
	 	function checkLoginStatus(){
		$user_data = $this->ci->session->userdata('user_data');//get session
		
		//echo "<pre>";print_r($session_data);echo"</pre>";
		/*
		 * 登入維持24小時
		 */
		$expire_minute = 60*60*24;
		
		/*
		 * 超過登入時效
		 */
		//echo time();exit;
        if (isset($user_data['LOGIN_INFO']['LAST_LOGIN_TIME']) && (time() - $user_data['LOGIN_INFO']['LAST_LOGIN_TIME'] > $expire_minute)) {
            $this->ci->session->sess_destroy();// destroy session data in storage
			
            return false;
        }
		elseif(!isset($user_data['LOGIN_INFO']['LAST_LOGIN_TIME']) || !isset($user_data['LOGIN_INFO']['LOGIN_CODE'])){
			return false;
		}
		else{
			return true;
		}
	}
	
}


?>